package com.geeklan.smart.ui.banner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geeklan.smart.data.model.BannerResponse;
import com.geeklan.smart.databinding.LayoutBannerItemBinding;
import com.geeklan.smart.ui.web.WebViewActivity;

import java.util.List;

public class BannerAdapter extends RecyclerView.Adapter<BannerAdapter.BannerViewHolder> {
    private Context mContext;
    private List<BannerResponse.Banner> bannerList;  // 这里的 bannerList 是图片的 URL 列表

    public BannerAdapter(Context context, List<BannerResponse.Banner> bannerList) {
        this.bannerList = bannerList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public BannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BannerViewHolder(LayoutBannerItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BannerViewHolder holder, int position) {
        // 获取图片 URL
        BannerResponse.Banner banner = bannerList.get(position);

        // 使用 Glide 加载图片到 ImageView
        Glide.with(mContext)
                .load(banner.getImgUrl())
                .into(holder.mBinding.bannerImage);  // 将图片加载到 ImageView 中

        holder.mBinding.bannerImage.setOnClickListener(v -> {
            if(banner.getJumpType() == 1) {
                WebViewActivity.startActivity(mContext, banner.getHrefUrl());
            }
        });
    }

    @Override
    public int getItemCount() {
        return bannerList.size();
    }

    public static class BannerViewHolder extends RecyclerView.ViewHolder {
        LayoutBannerItemBinding mBinding;

        public BannerViewHolder(LayoutBannerItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }
}

